<template>
  <div class="user-manage">
    <el-card class="top-contral">
      <div class="excel-contral">
        <el-button type="primary" @click="importExcel" v-permission="['importUser']">excel导入</el-button>
        <el-button type="success" @click="exportExcel">excel导出</el-button>
      </div>
    </el-card>

    <el-card>
      <el-table style="width: 100%" border :data="tableData">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="联系方式" prop="mobile"></el-table-column>
        <el-table-column label="头像" align="center">
          <template v-slot="{ row }">
            <el-avatar :src="row.avatar" :preview-src-list="[row.avatar]" />
          </template>
        </el-table-column>
        <el-table-column label="角色">
          <template v-slot="{ row }">
            <div v-if="row.role && row.role.length > 0">
              <el-tag
                size="small"
                type="success"
                v-for="(item, index) in row.role"
                :key="index"
                >{{ item.title }}</el-tag
              >
            </div>
            <div v-else>未知</div>
          </template>
        </el-table-column>
        <el-table-column label="开通时间">
          <template v-slot="{ row }">
            {{ $filter.dateFilter(row.openTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220px" fixed="right">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="handleDetail(row)"
              >查看</el-button
            >
            <el-button type="info" size="mini" @click="handleEditRole(row)" v-permission="['distributeRole']"
              >角色</el-button
            >
            <el-button type="danger" size="mini" @click="handleDelete(row)" v-permission="['removeUser']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分頁組件 -->
      <el-pagination
        class="pagination"
        :page-size="size"
        :page-sizes="[2, 5, 10, 20]"
        :total="total"
        :current-page="page"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </el-card>

    <export-to-excel v-model="exportToExcelVisable" />
    <role v-model="roleVisable" :id="roleId" @updateRole="updateRole"/>
  </div>
</template>

<script setup>
import { ref, watch, onActivated } from 'vue'
import { getUserManageList, userDelectById } from '@/api/user-manage'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import ExportToExcel from './components/export-to-excel'
import Role from '@/components/role'
const router = useRouter()
// import dayjs from 'dayjs'
const total = ref(1)
const page = ref(1)
const tableData = ref([])
const size = ref(2)
const roleVisable = ref(false)
const roleId = ref('')
const exportToExcelVisable = ref(false)
const getData = async () => {
  const result = await getUserManageList({
    page: page.value,
    size: size.value
  })
  console.log(result)
  tableData.value = result.list
  total.value = result.total
}

watch(
  [page, size],
  ([newPage, newSize]) => {
    getData()
  },
  {
    immediate: true
  }
)

onActivated(() => {
  getData()
})
const importExcel = () => {
  router.push('/user/import')
}
// const fmtDate = (date) => {
//   return dayjs(Number(date)).format('YYYY-MM-DD')
// }

// 当前页码改变时
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
}

// 当前size改变时
const handleSizeChange = (currentSize) => {
  size.value = currentSize
}

// 查看用户详情
const handleDetail = ({ _id: id }) => {
  router.push(`/user/info/${id}`)
}

// 删除当前用户
const handleDelete = ({ username, _id: id }) => {
  ElMessageBox.confirm(`确认删除${username}吗？`).then(async () => {
    await userDelectById(id)
    ElMessage.success('删除成功')
    getData()
  })
}

// 编辑用户角色
const handleEditRole = ({ _id: id }) => {
  roleId.value = id
  roleVisable.value = true
}

// 点击导出excel
const exportExcel = () => {
  exportToExcelVisable.value = true
}

const updateRole = () => {
  getData()
}
</script>

<style lang="scss" scoped>
.user-manage {
  .top-contral {
    margin-bottom: 20px;
    .excel-contral {
      display: flex;
      justify-content: flex-end;
    }
  }
  .pagination {
    margin-top: 20px;
    margin-left: 50%;
    transform: translateX(-50%);
  }
}
</style>
